package com.lsh.payment.core.service.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.payment.core.dao.redis.RedisStringDao;
import com.lsh.payment.core.model.config.PayConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Project Name: lsh-payment
 *
 * @author by peter on 17/12/22.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
public class RedisConfigService {

    @Autowired
    private ConfigService configService;

    @Autowired
    private RedisStringDao redisStringDao;


    public JSONObject getConfigContent(Integer configId) {

        PayConfig config = this.configService.getConfigOne(configId);
        if (config == null) {
            return null;
        }

        JSONObject content = JSON.parseObject(config.getContent());

        return content;
    }

    public String getCmSnValue(String cmSnKey) {

        String cmSn = this.redisStringDao.get(cmSnKey);

        if (StringUtils.isBlank(cmSn)) {
            JSONObject content = this.getConfigContent(1);

            if (content == null) {
                return "";
            }

            cmSn = content.getString("terminal_sn");

            this.redisStringDao.set(cmSnKey, cmSn);
        }

        return cmSn;
    }

    public String getCmKeyValue(String cmKeyKey) {

        String cmKey = this.redisStringDao.get(cmKeyKey);

        if (StringUtils.isBlank(cmKey)) {
            JSONObject content = this.getConfigContent(1);

            if (content == null) {
                return "";
            }

            cmKey = content.getString("terminal_key");

            this.redisStringDao.set(cmKeyKey, cmKey);
        }

        return cmKey;
    }


}
